package com.jsjosem.aftersales.ui.worktickets;

import static com.jsjosem.aftersales.utils.CommonUtils.getProvincesCity;
import static com.jsjosem.aftersales.utils.CommonUtils.selectTime;
import static com.jsjosem.aftersales.utils.CommonUtils.setSpinnerAdapter;
import static com.zx.sdk.api.ZXManager.TAG;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.api.CustomerApi;
import com.jsjosem.aftersales.api.CustomerUserApi;
import com.jsjosem.aftersales.api.RetrofitClient;
import com.jsjosem.aftersales.data.request.CustomerUserRequest;
import com.jsjosem.aftersales.data.response.BaseResponse;
import com.jsjosem.aftersales.data.response.CityResponse;
import com.jsjosem.aftersales.data.response.CustomerResponse;
import com.jsjosem.aftersales.request.AsyncRequest;
import com.jsjosem.aftersales.utils.ImmUtils;

import retrofit2.Call;
import retrofit2.Retrofit;

public class CreateCustomerUserActivity extends AppCompatActivity {

    private ImageView imgBack;
    private EditText userNameET, userPhoneET, userMailET, userPositionET;
    private EditText officeTelephoneET, userBirthET, projectRoleET, detailsAddressET;
    private TextView userAddressTv;
    private View userBirthView;
    private Spinner userGenderSp;
    private Button commit;
    private CheckBox defaultContact;
    private ConstraintLayout constraintLayout;
    private Intent intent;
    private CustomerResponse customer;
    private final CityResponse province = new CityResponse();
    private final CityResponse city = new CityResponse();
    private final CityResponse county = new CityResponse();
    private final String[] gender = {"男", "女"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_customer_user);
        initView();
        initData();

        ImmUtils.setCloseKeyboard(constraintLayout, this);
        ImmUtils.setEditTextHintFontSize(userNameET, 14, "请输入姓名");
        ImmUtils.setEditTextHintFontSize(userPhoneET, 14, "建议使用手机号,可以接受短信通知");
        ImmUtils.setEditTextHintFontSize(userMailET, 14, "请输入邮箱");
        ImmUtils.setEditTextHintFontSize(userPositionET, 14, "请输入职务");
        ImmUtils.setEditTextHintFontSize(userAddressTv, 14, "请选择地址");
        ImmUtils.setEditTextHintFontSize(officeTelephoneET, 14, "请输入办公电话");
        ImmUtils.setEditTextHintFontSize(userBirthET, 14, "请选择出生日期");
        ImmUtils.setEditTextHintFontSize(projectRoleET, 14, "请输入项目角色");
        ImmUtils.setEditTextHintFontSize(detailsAddressET, 14, "请输入详细地址");

        userBirthView.setOnClickListener(v -> {
            selectTime(this, userBirthET);
        });

        userAddressTv.setOnClickListener(v -> {
            getProvincesCity(this, userAddressTv, province, city, county);
        });

        setSpinnerAdapter(this, userGenderSp, gender, 0);

        commit.setOnClickListener(v -> {
            CustomerUserRequest customerUser = new CustomerUserRequest();
            if (userNameET.getText().toString().isEmpty()) {
                showToast("请输入姓名");
                return;
            } else {
                customerUser.setName(userNameET.getText().toString());
            }
            if (userPhoneET.getText().toString().isEmpty()) {
                showToast("请输入手机号");
                return;
            } else {
                customerUser.setPhone(userPhoneET.getText().toString());
            }
            if (officeTelephoneET.getText().toString().isEmpty()) {
                showToast("请输入办公电话");
                return;
            } else {
                customerUser.setOfficePhone(officeTelephoneET.getText().toString());
            }
            if (userMailET.getText().toString().isEmpty()) {
                showToast("请输入邮箱");
                return;
            } else {
                customerUser.setEmail(userMailET.getText().toString());
            }
            if (userBirthET.getText().toString().isEmpty()) {
                showToast("请选择出生日期");
                return;
            } else {
                customerUser.setBirthday(userBirthET.getText().toString());
            }
            if (userPositionET.getText().toString().isEmpty()) {
                showToast("请输入职务");
                return;
            } else {
                customerUser.setDuty(userPositionET.getText().toString());
            }
            if (projectRoleET.getText().toString().isEmpty()) {
                showToast("请输入项目角色");
                return;
            } else {
                customerUser.setProjectRole(projectRoleET.getText().toString());
            }
            if (userAddressTv.getText().toString().isEmpty()) {
                showToast("请选择地址");
                return;
            } else {
                customerUser.setProvinceId(Long.valueOf(province.getCitycode()));
                customerUser.setCityId(Long.valueOf(city.getCitycode()));
                customerUser.setCountyId(Long.valueOf(county.getCitycode()));
            }
            if (detailsAddressET.getText().toString().isEmpty()) {
                showToast("请输入详细地址");
                return;
            } else {
                customerUser.setAddress(detailsAddressET.getText().toString());
            }
            if (defaultContact.isChecked()) {
                customerUser.setIsDefault("1");
            } else {
                customerUser.setIsDefault("0");
            }
            customerUser.setGender(userGenderSp.getSelectedItem().toString());
            customerUser.setCustomerId(customer.getId().toString());
            createCustomerUser(customerUser);
        });

        imgBack.setOnClickListener(v -> finish());
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initView() {
        imgBack = findViewById(R.id.work_order_img_back);
        userNameET = findViewById(R.id.user_name);
        userPhoneET = findViewById(R.id.user_phone);
        userMailET = findViewById(R.id.user_mail);
        userPositionET = findViewById(R.id.user_position);
        userAddressTv = findViewById(R.id.user_address);
        constraintLayout = findViewById(R.id.constraintLayout);
        userGenderSp = findViewById(R.id.user_gender);
        officeTelephoneET = findViewById(R.id.office_telephone);
        userBirthET = findViewById(R.id.user_birth);
        projectRoleET = findViewById(R.id.project_role);
        detailsAddressET = findViewById(R.id.details_address);
        userBirthView = findViewById(R.id.user_birth_view);
        commit = findViewById(R.id.bt_commit);
        defaultContact = findViewById(R.id.default_contact);
    }

    private void initData() {
        intent = getIntent();
        customer = (CustomerResponse) intent.getSerializableExtra("customer");
    }


    private void getData() {
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private void createCustomerUser(CustomerUserRequest customerUser) {
        Retrofit mRetrofit = RetrofitClient.getInstance();
        CustomerUserApi customerUserApi = mRetrofit.create(CustomerUserApi.class);
        Call<BaseResponse<Void>> createCustomerUser = customerUserApi.createCustomerUser(customerUser);
        AsyncRequest<BaseResponse<Void>> request = new AsyncRequest<>();
        request.sendRequest(createCustomerUser, res -> {
            showToast("创建成功");
            finish();
        }, err -> {
            Log.e(TAG, "createCustomer: " + err.toString());
        });

    }
}